package org.weed.gadgets.audio.asf;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.Arrays;

import org.weed.gadgets.util.ByteUtils;

public class GUID {
	private final byte[] guid; // 16 bytes
	
	public GUID(byte[] guid) {
		this.guid = guid;
	}
	
	public GUID(byte[] v1, byte[] v2, byte[] v3, byte[] v4) {
		guid = new byte[ASFConstants.GUID];
		System.arraycopy(v1, 0, guid, 0, 4);
		System.arraycopy(v2, 0, guid, 4, 2);
		System.arraycopy(v3, 0, guid, 6, 2);
		System.arraycopy(v4, 0, guid, 8,  8);
	}
	
	public byte[] byteArray() {
		return guid;
	}
	
	public static GUID newGUID(String hex) {
		hex = hex.replace("-", "");
		if (hex == null || hex.length() != 32) throw new IllegalArgumentException("Invalid GUID [" + hex + "]");
		BigInteger bigInt = new BigInteger(hex,16);
		return new GUID(bigInt.toByteArray());
	}
	
	public static GUID parseGUID(byte[] bytes) {
		if (bytes == null || bytes.length < ASFConstants.GUID) throw new IllegalArgumentException("GUID must contains 16 bytes.");
		return new GUID(
				new byte[] {bytes[3], bytes[2], bytes[1], bytes[0]},
				new byte[] {bytes[5], bytes[4]},
				new byte[] {bytes[7], bytes[6]},
				Arrays.copyOfRange(bytes, 8, 16)
			);
	}
	
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append(ByteUtils.toHex(guid, 0, 4)).append("-")
				.append(ByteUtils.toHex(guid, 4, 2)).append("-")
				.append(ByteUtils.toHex(guid, 6, 2)).append("-")
				.append(ByteUtils.toHex(guid, 8, 2)).append("-")
				.append(ByteUtils.toHex(guid, 10, 6));
		return builder.toString();
	}
	
	public boolean equals(GUID guid) {
		if (guid != null) {
			return Arrays.equals(this.guid, guid.byteArray());
		}
		return false;
	}
	
	public int hashCode() {
		return Arrays.hashCode(guid);
	}
	
	public static final GUID ASF_HEADER_OBJECT = GUID.newGUID("75B22636-668E-11CF-A6D9-00AA0062CE6C");
	public static final GUID ASF_DATA_OBJECT = GUID.newGUID("33000890-E5B1-11CF-89F4-00A0C90349CB");
	public static final GUID ASF_INDEX_OBJECT = GUID.newGUID("D6E229D3-35DA-11D1-9034-00A0C90349BE");
	
	public static final GUID EXTENDED_CONTENT_DESCRIPTION_OBJECT = GUID.newGUID("D2D0A440-E307-11D2-97F0-00A0C95EA850");
	public static final GUID FILE_PROPERTIES_OBJECT = GUID.newGUID("8CABDCA1-A947-11CF-8EE4-00C00C205365");
	public static final GUID HEADER_EXTENSION_OBJECT = GUID.newGUID("5FBF03B5-A92E-11CF-8EE3-00C00C205365");
	public static final GUID CODEC_LIST_OBJECT = GUID.newGUID("86D15240-311D-11D0-A3A4-00A0C90348F6");
	public static final GUID STREAM_PROPERTIES_OBJECT = GUID.newGUID("B7DC0791-A9B7-11CF-8EE6-00C00C205365");
	public static final GUID STREAM_BITRATE_PROPERTIES_OBJECT = GUID.newGUID("7BF875CE-468D-11D1-8D82-006097C9A2B2");
	public static final GUID CONTENT_DESCRIPTION_OBJECT = GUID.newGUID("75B22633-668E-11CF-A6D9-00AA0062CE6C");
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		System.out.println(GUID.newGUID("75B22630668E11CFA6D900AA0062CE6C"));
		
	}
}
